//
//  ReportMoreViewController.m
//  Hxcsfw
//
//  Created by 企友软mac on 2018/1/11.
//  Copyright © 2018年 企友软mac. All rights reserved.
//

#import "ReportMoreViewController.h"
#import "ReportMoreCell.h"
#import "DiagramModel.h"

@interface ReportMoreViewController ()<UITableViewDelegate,UITableViewDataSource>{
    int j;
}

@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *arrData;

@end

@implementation ReportMoreViewController

- (void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    j=1;
    self.arrData = [NSMutableArray array];
    [self.view addSubview:self.tableView];
    [self.tableView.superview insertSubview:[UIView new] atIndex:0];
    [self reportVcMoreDataRequestStrNum:@"1" isRefrsh:YES];
    [self notifyRequestListData];
}

- (void)noWifiRefershingEnd {
    [self.tableView.header endRefreshing];
    [self.tableView.footer endRefreshing];
    [WShareUtil sharedData].isShowWait = YES;

}

- (void)notifyRequestListData{

    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self reportVcMoreDataRequestStrNum:@"1" isRefrsh:YES];
        j = 1;
    }];

    self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        NSString *num = [NSString stringWithFormat:@"%d",++j];
        [self reportVcMoreDataRequestStrNum:num isRefrsh:NO];
    }];
}

- (UITableView *)tableView {
    if (_tableView == nil) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, WSCREENH_WIDTH, WSCREENH_HEIGHT - 64) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.estimatedRowHeight=70.f;
        _tableView.showsVerticalScrollIndicator = YES;
        _tableView.bounces = YES;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.arrData count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *identCell = @"HomeListCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identCell];

    if (cell == nil) {
        ReportMoreCell *reportMoreCell = [[[NSBundle mainBundle] loadNibNamed:@"ReportMoreCell" owner:self options:nil] lastObject];

        if (self.arrData.count > 0 && self.arrData) {
            reportMoreCell.lbTitle.text = [self.arrData[indexPath.row] strTitle];
            reportMoreCell.lbNum.text = [self.arrData[indexPath.row] fbsj];
        }
        cell = reportMoreCell;
    }

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

//    VideoDetailViewController *videoDetailVc = [[VideoDetailViewController alloc] init];
//    videoDetailVc.strId = [self.arrData[indexPath.row] strId];
//    [self.navigationController pushViewController:videoDetailVc animated:YES];
}

- (void)reportVcMoreDataRequestStrNum:(NSString *)pageNum isRefrsh:(BOOL)isRefrsh{

    [WShareUtil sharedData].isShowWait = NO;

    NSDictionary *parametersDict = @{@"mode":@"native",@"service":@"xxfb",@"content":@{@"revert":@"paginate",@"colcode":_colcode,@"pagesize":@"10",@"pagenumber":pageNum}};
    NSString *secret = [NSString stringWithFormat:@"%@",[WHttpTool convertToJsonData:parametersDict]];
    NSDictionary *paramsDic = @{@"params":[CommonFunc encryptWithText:secret]};


    [WHttpTool POST:[WShareUtil getServerURL:nil] parameters:@{@"params":[CommonFunc encryptWithText:secret]} success:^(id responseObject) {
        WLog(@"------%@",responseObject);
        NSString *responseStr = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        NSDictionary *JsonDic = [WHttpTool dictionaryWithJsonString:responseStr];
        NSString *messageStr = [JsonDic objectForKey:@"message"];
        NSString *successStr = [JsonDic objectForKey:@"success"];
        // 解密请求回的数据（字典）
        NSString *returnStr = [CommonFunc decryptWithText:messageStr];
        // json字符串转字典
        NSDictionary *returnDic = [WHttpTool dictionaryWithJsonString:returnStr];

        if (isRefrsh) {
            [self.arrData removeAllObjects];
        }
        for (NSDictionary *dic in returnDic[@"data"]) {
            DiagramModel *diagramModel = [DiagramModel mj_objectWithKeyValues:dic];
            [self.arrData addObject:diagramModel];
        }
        [self.tableView reloadData];

        // --- 图片式提醒
        UIView *view = [self.tableView viewWithTag:10000];
        [view removeFromSuperview];
        HHNonePage *_nonePage = [[HHNonePage alloc]init];
        _nonePage = nil;
        if (self.arrData.count == 0) {
            _nonePage = [[HHNonePage alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
            _nonePage.tag = 10000;
            [self.tableView addSubview:_nonePage];
        }
        if (self.arrData.count < 10) {
            // 拿到当前的上拉刷新控件，变为没有更多数据的状态
            [self.tableView.footer endRefreshingWithNoMoreData];
        }

        [self noWifiRefershingEnd];

    } failure:^(NSError *error) {
        [self noWifiRefershingEnd];
        WLog(@"失败");
    } showProgress:YES];
}

@end
